﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8" />
    <title>Ping: Ping.cpp Source File</title>
    <link href="/Content/Help.css" rel="stylesheet" type="text/css" />
    <link href="/Content/css/tabs.css" rel="stylesheet" type="text/css" />
    <link href="/Content/css/doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
    <div id="header">
        <img id="logo" alt="StateForge" src="/Content/Images/StateForgeLogo.png" />
        <div id="title">
            <h1>
                Examples using state machines generated by StateBuilderCpp
            </h1>
            <h3>
                Ping: Ping.cpp Source File
            </h3>
        </div>
        <div id="menucontainer">
            <ul id="menu">
                <li><a href="/">Home</a></li>
                <li><a href="/Home/Download">Downloads</a></li>
                <li><a href="/Help/state-machine-tools.aspx">Help</a></li>
                <li><a href="/Home/Faq">F.A.Q</a></li>
                <li><a href="/Home/About">Contact us</a></li>
            </ul>
        </div>
    </div>
<!-- Generated by Doxygen 1.7.4 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>File&#160;Members</span></a></li>
    </ul>
  </div>
<div class="header">
  <div class="headertitle">
<div class="title">Ping.cpp</div>  </div>
</div>
<div class="contents">
<a href="_ping_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 
<a name="l00002"></a>00002 <span class="preprocessor">#include &lt;iostream&gt;</span>
<a name="l00003"></a>00003 
<a name="l00004"></a>00004 <span class="preprocessor">#include &lt;QtCore/QCoreApplication&gt;</span>
<a name="l00005"></a>00005 <span class="preprocessor">#include &lt;QtCore/QTimer&gt;</span>
<a name="l00006"></a>00006 <span class="preprocessor">#include &lt;QtCore/QDebug&gt;</span>
<a name="l00007"></a>00007 <span class="preprocessor">#include &lt;QtCore/QDir&gt;</span>
<a name="l00008"></a>00008 
<a name="l00009"></a>00009 <span class="preprocessor">#include &lt;QsLog.h&gt;</span>
<a name="l00010"></a>00010 <span class="preprocessor">#include &lt;QsLogDest.h&gt;</span>
<a name="l00011"></a>00011 
<a name="l00012"></a>00012 <span class="preprocessor">#include &lt;SmQtObserver.h&gt;</span>
<a name="l00013"></a>00013 
<a name="l00014"></a>00014 <span class="preprocessor">#include &quot;<a class="code" href="_ping_8h.html">Ping.h</a>&quot;</span>
<a name="l00015"></a>00015 <span class="preprocessor">#include &quot;<a class="code" href="_udp_echo_client_8h.html">UdpEchoClient.h</a>&quot;</span>
<a name="l00016"></a>00016 <span class="preprocessor">#include &lt;PingFsm.h&gt;</span> <span class="comment">// Generated header</span>
<a name="l00017"></a>00017 
<a name="l00018"></a><a class="code" href="class_ping.html#af5927f79e7c2503e999e7a5b936ebc0e">00018</a> <a class="code" href="class_ping.html#af5927f79e7c2503e999e7a5b936ebc0e">Ping::Ping</a>() :
<a name="l00019"></a>00019   m_iRx(0),
<a name="l00020"></a>00020   m_iTx(0),
<a name="l00021"></a>00021   m_iCount(5),
<a name="l00022"></a>00022   m_lTimeout(1000)
<a name="l00023"></a>00023 {
<a name="l00024"></a>00024     <a class="code" href="class_ping.html#add2b5072281b54697f29d2dee19c3727">logInit</a>();
<a name="l00025"></a>00025     <a class="code" href="class_ping.html#a087366992f27a6fcfa44de07580e1367">m_pUdpEchoClient</a> = <span class="keyword">new</span> <a class="code" href="class_udp_echo_client.html">UdpEchoClient</a>(<span class="keyword">this</span>);
<a name="l00026"></a>00026     QObject::connect(<a class="code" href="class_ping.html#a087366992f27a6fcfa44de07580e1367">m_pUdpEchoClient</a>,
<a name="l00027"></a>00027                      SIGNAL(reply()),
<a name="l00028"></a>00028                      <span class="keyword">this</span>,
<a name="l00029"></a>00029                      SLOT(<a class="code" href="class_ping.html#a4a82187b6e77afd265c97a7d61d81be5">onReply</a>()));
<a name="l00030"></a>00030 
<a name="l00031"></a>00031     <span class="comment">// Generated PingContext</span>
<a name="l00032"></a>00032     <a class="code" href="class_ping.html#a71b3e4c517fe1aa542f88d360a5a0b1c">m_pPingContext</a> = <span class="keyword">new</span> PingContext(*<span class="keyword">this</span>, <span class="keyword">this</span>);
<a name="l00033"></a>00033     <a class="code" href="class_ping.html#a71b3e4c517fe1aa542f88d360a5a0b1c">m_pPingContext</a>-&gt;EnterInitialState();
<a name="l00034"></a>00034     <a class="code" href="class_ping.html#a71b3e4c517fe1aa542f88d360a5a0b1c">m_pPingContext</a>-&gt;SetObserver(SmQtObserver::GetInstance());
<a name="l00035"></a>00035     QObject::connect(<a class="code" href="class_ping.html#a71b3e4c517fe1aa542f88d360a5a0b1c">m_pPingContext</a>,
<a name="l00036"></a>00036                      SIGNAL(end()),
<a name="l00037"></a>00037                      <span class="keyword">this</span>,
<a name="l00038"></a>00038                      SLOT(<a class="code" href="class_ping.html#a8f51833ce898be6744faeebdb73c8c66">contextEnd</a>()));
<a name="l00039"></a>00039 }
<a name="l00040"></a>00040 
<a name="l00041"></a><a class="code" href="class_ping.html#a3e621888861ea796d21647f7d631d388">00041</a> <a class="code" href="class_ping.html#a3e621888861ea796d21647f7d631d388">Ping::~Ping</a>()
<a name="l00042"></a>00042 {
<a name="l00043"></a>00043     QLOG_INFO();
<a name="l00044"></a>00044 }
<a name="l00045"></a>00045 
<a name="l00046"></a><a class="code" href="class_ping.html#aab6c4b6fdb9ae51a84f9c06a6fa555da">00046</a> <span class="keywordtype">void</span> <a class="code" href="class_ping.html#aab6c4b6fdb9ae51a84f9c06a6fa555da">Ping::send</a>()
<a name="l00047"></a>00047 {
<a name="l00048"></a>00048     <a class="code" href="class_ping.html#aeb12e44de6a353771f72035724a6db7b">m_iTx</a>++;
<a name="l00049"></a>00049     <a class="code" href="class_ping.html#a087366992f27a6fcfa44de07580e1367">m_pUdpEchoClient</a>-&gt;<a class="code" href="class_udp_echo_client.html#af9885baea55efc4fc611605e087d6427">send</a>();
<a name="l00050"></a>00050 }
<a name="l00051"></a>00051 
<a name="l00052"></a><a class="code" href="class_ping.html#add2b5072281b54697f29d2dee19c3727">00052</a> <span class="keywordtype">void</span> <a class="code" href="class_ping.html#add2b5072281b54697f29d2dee19c3727">Ping::logInit</a>()
<a name="l00053"></a>00053 {
<a name="l00054"></a>00054    QCoreApplication *pApp = QCoreApplication::instance();
<a name="l00055"></a>00055    QsLogging::Logger&amp; logger = QsLogging::Logger::instance();
<a name="l00056"></a>00056    logger.setLoggingLevel(QsLogging::TraceLevel);
<a name="l00057"></a>00057    <span class="comment">//Log to file</span>
<a name="l00058"></a>00058    <span class="keyword">const</span> QString logPath(QDir(pApp-&gt;applicationDirPath()).filePath(<span class="stringliteral">&quot;Log.txt&quot;</span>));
<a name="l00059"></a>00059    QsLogging::DestinationPtr fileDestination(
<a name="l00060"></a>00060       QsLogging::DestinationFactory::MakeFileDestination(logPath) );
<a name="l00061"></a>00061    logger.addDestination(fileDestination);
<a name="l00062"></a>00062    <span class="comment">//Log to standard output</span>
<a name="l00063"></a>00063    QsLogging::DestinationPtr debugDestination = QsLogging::DestinationFactory::MakeDebugOutputDestination();
<a name="l00064"></a>00064    logger.addDestination(debugDestination);
<a name="l00065"></a>00065 }
<a name="l00066"></a>00066 
<a name="l00067"></a><a class="code" href="class_ping.html#a4a82187b6e77afd265c97a7d61d81be5">00067</a> <span class="keywordtype">void</span> <a class="code" href="class_ping.html#a4a82187b6e77afd265c97a7d61d81be5">Ping::onReply</a>()
<a name="l00068"></a>00068 {
<a name="l00069"></a>00069    <a class="code" href="class_ping.html#a452b351c5f040a88c4e857901260c062">m_iRx</a>++;
<a name="l00070"></a>00070    <a class="code" href="class_ping.html#a71b3e4c517fe1aa542f88d360a5a0b1c">m_pPingContext</a>-&gt;EvPingReply();
<a name="l00071"></a>00071 }
<a name="l00072"></a>00072 
<a name="l00073"></a><a class="code" href="class_ping.html#a8f51833ce898be6744faeebdb73c8c66">00073</a> <span class="keywordtype">void</span> <a class="code" href="class_ping.html#a8f51833ce898be6744faeebdb73c8c66">Ping::contextEnd</a>()
<a name="l00074"></a>00074 {
<a name="l00075"></a>00075     QLOG_INFO();
<a name="l00076"></a>00076     <a class="code" href="class_ping.html#a10309b89388a484e25fecc7dcac75273">printStatistics</a>();
<a name="l00077"></a>00077     QCoreApplication::exit(0);
<a name="l00078"></a>00078 }
<a name="l00079"></a>00079 
<a name="l00080"></a><a class="code" href="class_ping.html#adf8e06c5e894d2f04e5a003af7fb1861">00080</a> <span class="keywordtype">void</span> <a class="code" href="class_ping.html#adf8e06c5e894d2f04e5a003af7fb1861">Ping::start</a>()
<a name="l00081"></a>00081 {
<a name="l00082"></a>00082     QLOG_INFO();
<a name="l00083"></a>00083     <a class="code" href="class_ping.html#a71b3e4c517fe1aa542f88d360a5a0b1c">m_pPingContext</a>-&gt;EvStart();
<a name="l00084"></a>00084 }
<a name="l00085"></a>00085 
<a name="l00086"></a><a class="code" href="class_ping.html#a10309b89388a484e25fecc7dcac75273">00086</a> <span class="keywordtype">void</span> <a class="code" href="class_ping.html#a10309b89388a484e25fecc7dcac75273">Ping::printStatistics</a>()
<a name="l00087"></a>00087 {
<a name="l00088"></a>00088     QLOG_INFO() &lt;&lt; <span class="stringliteral">&quot;: &quot;</span> &lt;&lt; <a class="code" href="class_ping.html#a452b351c5f040a88c4e857901260c062">m_iRx</a> &lt;&lt; <span class="stringliteral">&quot;/&quot;</span> &lt;&lt;  <a class="code" href="class_ping.html#aeb12e44de6a353771f72035724a6db7b">m_iTx</a>;
<a name="l00089"></a>00089 
<a name="l00090"></a>00090 }
<a name="l00091"></a>00091 
<a name="l00092"></a>00092 <span class="preprocessor">#include &quot;Ping.moc&quot;</span>
</pre></div></div>
</div>
﻿<div id="footer">
    <hr />
    <p>
        Copyright &copy; 2011 StateForge.</p>
    <hr />
</div>
</body> </html> 
